﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.WindowsCE.Forms;
using Lilium.ComponentModel;
using Lilium.UI.Controls;

namespace Lilium.UI.Forms
{
    /*
    [Obsolete]
	public sealed class FormViewAdapter : Component, IViewport<System.Windows.Forms.Control>, IExtensionManager, ISupportInitialize
	{
		private const int SIPBarHeight = 26;

		public FormViewAdapter()
		{
			m_InputPanel = new InputPanel();
			m_InputPanel.EnabledChanged += new EventHandler(InputPanel_EnabledChanged);
		}

		public FormViewAdapter(IContainer container)
			: this()
		{
			if (container != null)
				container.Add(this);
		}

		#region public Panel Viewport

		private Panel m_Viewport;

		public Panel Viewport
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_Viewport;
			}
			[DebuggerNonUserCodeAttribute]
			set
			{
				if (m_Viewport != value)
				{
					UnbindViewport();
					m_Viewport = value;
					if (!InitializingComponent)
						BindViewport();
				}
			}
		}

		#endregion

		#region IExtensionManager Members

		public void AttachExtension<TOwner>(TOwner owner, IExtension<TOwner> extension)
		{
			if (extension == null) throw new ArgumentNullException("extension");

			extension.Attach(owner);

			ICollection<object> extensionCollection;
			if (!ManagedExtensions.TryGetValue(owner, out extensionCollection))
			{
				extensionCollection = new List<object>();
				ManagedExtensions.Add(owner, extensionCollection);

				var ownerComponent = owner as IComponent;
				if (ownerComponent != null)
				{
					ownerComponent.Disposed += ManagedComponent_Disposed;
				}
			}
			extensionCollection.Add(extension);
		}

		public void DetachExtension<TOwner>(TOwner owner, IExtension<TOwner> extension)
		{
			if (extension == null) throw new ArgumentNullException("extension");

			ICollection<object> extensionCollection;
			if (ManagedExtensions.TryGetValue(owner, out extensionCollection))
			{
				if (extensionCollection.Contains(extension))
				{
					extension.Detach(owner);
					extensionCollection.Remove(extension);
					if (extensionCollection.Count == 0)
					{
						ManagedExtensions.Remove(owner);
						var ownerComponent = owner as IComponent;
						if (ownerComponent != null)
						{
							ownerComponent.Disposed -= ManagedComponent_Disposed;
						}
					}
					return;
				}
			}
			throw new InvalidOperationException(Resources.FormViewAdapter_ExtensionNotRegisteredOrNotAttached);
		}

		public IEnumerable<TExtension> GetExtensions<TExtension, TOwner>(TOwner owner)
		{
			ICollection<object> extensionCollection;
			if (ManagedExtensions.TryGetValue(owner, out extensionCollection))
				return extensionCollection.OfType<TExtension>();
			else
				return Enumerable.Empty<TExtension>();
		}

		#endregion

		#region ISupportInitialize Members

		public void BeginInit()
		{
			if (!InitializingComponent)
			{
				InitializingComponent = true;
			}
		}

		public void EndInit()
		{
			if (InitializingComponent)
			{
				InitializingComponent = false;
				BindViewport();
			}
		}

		#endregion

		#region IViewport Members

		public ILabel AddLabel(string text)
		{
			var label = new Label
			{
				Text = text,
			};
			AddControl(label);
            return new LabelAdapter(label);
		}

        public IPushButton AddPushButton(string label)
        {
            var button = new Button()
            {
                Text = label,
            };
            AddControl(button);
            return new ButtonAdapter(button);
        }

        public ITextField AddTextField()
        {
            var textBox = new TextBox();
            AddControl(textBox);
            return new TextBoxAdapter(textBox);
        }

		public void BeginInitialize()
		{
			if (!InitializingViewport)
			{
				EnsureBound();
				InitializingViewport = true;

                ControlStack = new Stack<System.Windows.Forms.Control>();
				Viewport.SuspendLayout();
			}
		}

		public void Clear()
		{
			EnsureBound();
			Viewport.Controls.Clear();
		}

		public void EndInitialize()
		{
			if (InitializingViewport)
			{
				EnsureBound();
				InitializingViewport = false;

				var controls = Viewport.Controls;
				while (ControlStack.Count != 0)
				{
					controls.Add(ControlStack.Pop());
				}
				var focusedControlIndex = controls.Count - 1;
				while (focusedControlIndex >= 0)
					if (controls[focusedControlIndex].Focus())
						break;
					else
						focusedControlIndex--;
				ControlStack = null;
				Viewport.ResumeLayout();
			}
		}

		public string ViewName
		{
			get
			{
				EnsureBound();
				return Form != null ? Form.Text : null;
			}
			set
			{
				EnsureBound();
				if (Form != null)
					Form.Text = value;
			}
		}

		#endregion

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				UnbindViewport();
				InputPanel.Dispose();
			}
			base.Dispose(disposing);
		}

        private void AddControl(System.Windows.Forms.Control control)
		{
			if (control == null) throw new ArgumentNullException("control");

			control.Dock = DockStyle.Top;

			if (InitializingViewport)
			{
				ControlStack.Push(control);
			}
			else
			{
				EnsureBound();
				var controls = Viewport.Controls;
				controls.Add(control);
			}
		}

		private void AdjustViewportSize()
		{
			var viewportSize = InputPanel.VisibleDesktop.Size;
			if (!InputPanel.Enabled)
				viewportSize.Height -= SIPBarHeight;
			Viewport.Size = viewportSize;
		}

		private void BindViewport()
		{
			var viewPort = Viewport;
			if (viewPort != null)
			{
				viewPort.ParentChanged += ViewPort_ParentChanged;
				Form = FindForm(viewPort.Parent);
			}
		}

		private void EnsureBound()
		{
			if (Viewport == null)
				throw new InvalidOperationException(string.Format(
					Resources.Culture,
					Resources.FormViewAdapter_FormViewAdapterUnbound));
		}

        private Form FindForm(System.Windows.Forms.Control control)
		{
			while (control != null)
			{
				var form = control as Form;
				if (form != null)
					return form;
				control = control.Parent;
			}
			return null;
		}

		private void UnbindViewport()
		{
			var viewPort = Viewport;
			if (viewPort != null)
			{
				viewPort.ParentChanged -= ViewPort_ParentChanged;
				Form = null;
			}
		}

		#region Event handlers

		private void Form_Activated(object sender, EventArgs e)
		{
			AdjustViewportSize();
		}

		private void Form_Resize(object sender, EventArgs e)
		{
			AdjustViewportSize();
		}

		private void InputPanel_EnabledChanged(object sender, EventArgs e)
		{
			if (Viewport != null)
				AdjustViewportSize();
		}

		private void ManagedComponent_Disposed(object sender, EventArgs e)
		{	// расширения не отцепляются в случае уничтожения объекта, просто удаляется информация о регистрации.
			ManagedExtensions.Remove(sender);
		}

		private void ViewPort_ParentChanged(object sender, EventArgs e)
		{
			Form = FindForm(Viewport.Parent);
		}

		#endregion

		#region private Stack<Control> ControlStack

        private Stack<System.Windows.Forms.Control> m_ControlStack;

        private Stack<System.Windows.Forms.Control> ControlStack
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_ControlStack;
			}
			[DebuggerNonUserCodeAttribute]
			set
			{
				m_ControlStack = value;
			}
		}

		#endregion

		#region private Form Form

		private Form m_Form;

		private Form Form
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_Form;
			}
			[DebuggerNonUserCodeAttribute]
			set
			{
				if (m_Form != value)
				{
					if (m_Form != null)
					{
						m_Form.Activated -= Form_Activated;
						m_Form.Resize -= Form_Resize;
					}
					m_Form = value;
					if (value != null)
					{
						m_Form.Activated += Form_Activated;
						m_Form.Resize += Form_Resize;
					}
				}
			}
		}

		#endregion

		#region private bool InitializingComponent

		private bool m_InitializingComponent;

		private bool InitializingComponent
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_InitializingComponent;
			}
			[DebuggerNonUserCodeAttribute]
			set
			{
				m_InitializingComponent = value;
			}
		}

		#endregion

		#region private bool InitializingViewport

		private bool m_InitializingViewport;

		private bool InitializingViewport
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_InitializingViewport;
			}
			[DebuggerNonUserCodeAttribute]
			set
			{
				m_InitializingViewport = value;
			}
		}

		#endregion

		#region private InputPanel InputPanel

		private readonly InputPanel m_InputPanel;

		private InputPanel InputPanel
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_InputPanel;
			}
		}

		#endregion

		#region private Dictionary<Control, List<object>> ManagedExtensions

		private readonly Dictionary<object, ICollection<object>> m_ManagedExtensions = new Dictionary<object, ICollection<object>>();

		private Dictionary<object, ICollection<object>> ManagedExtensions
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_ManagedExtensions;
			}
		}

		#endregion

        #region IViewport Members

        public IControlFactory<System.Windows.Forms.Control> ControlFactory
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IViewport<Control> Members

        IControlFactory<System.Windows.Forms.Control> IViewport<System.Windows.Forms.Control>.ControlFactory
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
    */    
}